package com.sx.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author Administrator
 * @Description
 * @Date 2020/11/2 16:08
 *
 * 该案例：读文件
 */
public class NIOFileChannel02 {
    public static void main(String[] args) throws Exception{
        File file = new File("d://file.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

//        通过fileInputStream获取对应的通道
        FileChannel channel = fileInputStream.getChannel();

//        创建buffer
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());

//        将channel中的数据读取到buffer中(这里读取数据)
        channel.read(buffer);

//        将字节数据转换成String
        System.out.println(new String(buffer.array()));
        fileInputStream.close();
    }
}
